<template>
  <div>
    <ul class="ciry-list">
      <!-- handle处理 -->
      <li
        class="item"
        v-for="(item, key) of letters"
        :key="item"
        :ref="item"
        @touchstart="handleTouchStart"
        @touchmove="handleTouchMove"
        @touchend="handleTouchEnd"
        @click="handleletterClick"
      >
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "CityAiphabet",
  props: {
    cities: Object,
  },

  computed: {
    letters() {
      const letters = [];
      for (let i in this.cities) {
        letters.push(i);
      }
      return letters;
    },
  },
  data() {
    return {
      touchStatus: false,
      timer: null,
    };
  },
  methods: {
    handleletterClick(e) {
      this.$emit("change", e.target.innerText);
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      if (this.touchStatus) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          const startY = this.$refs["A"][0].offsetTop;
          const touchY = e.touches[0].clientY - 139;
          const index = Math.floor((touchY - startY) / 20);
          if (index >= 0 && index < this.letters.length) {
            this.$emit("change", this.letters[index]);
          }
        }, 16);
      }
    },
    handleTouchEnd() {
      this.touchStatus = false;
    },
  },
};
</script>

<style lang="stylus" scoped>
.ciry-list {
  position: absolute;
  top: 2.8rem;
  right: 0;
  width: 0.4rem;
}

.item {
  line-height: 0.41rem;
  text-align: center;
  color: #00bcd4;
}
</style>